#include "widget.h"
#include "ui_widget.h"

#include <QToolBox>
#include <QDebug>
#include <QPushButton>
#include <QVBoxLayout>
#include <QDialog>
#include <QLabel>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->resize(600, 480);

    // 初始化数据
    this->fList = {
            {"我的好友", {
                {1, "二狗", ":/images/touxiang.png", "aaa", "2024-09-01"},
                {2, "碎怂", ":/images/touxiang_1.png", "aaa", "2024-09-01"},
                {3, "翠花", ":/images/touxiang_2.png", "aaa", "2024-09-01"},
                {4, "钢弹", ":/images/touxiang_3.png", "aaa", "2024-09-01"},
                {5, "棒槌", ":/images/touxiang.png", "aaa", "2024-09-01"},
            }},
            {"黑名单", {
                {6, "小明", ":/images/icon-test.png", "aaa", "2024-09-01"},
                {7, "小红", ":/images/icon-test_1.png", "aaa", "2024-09-01"},
                {8, "韩没", ":/images/icon-test_2.png", "aaa", "2024-09-01"},
                {9, "李磊", ":/images/icontouxiang.png", "aaa", "2024-09-01"},
                {10, "海绵宝宝", ":/images/user__easyico.png", "aaa", "2024-09-01"},
            }},
     };

    QToolBox* qq = new QToolBox(this);
    qq->resize(200, 400);
    qq->move(10, 10);
    qq->setFrameShape(QFrame::Box);

    for (auto it = this->fList.begin(); it != this->fList.end(); it++)
    {
        QWidget* w = new QWidget;
        QVBoxLayout* v = new QVBoxLayout(w);
        v->setAlignment(Qt::AlignTop);
        for (FriendInfo item : it.value())
        {
            // 每得到一个好友信息，就创建一个按钮，并将好友信息设置到按钮上
            QPushButton* btn = new QPushButton;
            btn->setIcon(QIcon(item.avatar));
            btn->setText(item.nickname);
            v->addWidget(btn);

            // 给每一个好友按钮上设置信号和槽
            connect(btn, &QPushButton::clicked, [item](){
                QDialog dia;
                dia.resize(600, 400);
                QLabel * l = new QLabel(item.nickname, &dia);
                l->move(290, 10);
                dia.exec();
            });
        }

        qq->addItem(w, it.key());
    }
}

Widget::~Widget()
{
    delete ui;
}
